﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Metro.Kashikoi.Common
{
   public static class UserTokens
    {
       public static Dictionary<string, string> tokenDic = new Dictionary<string, string>();
       
       public static string RandomString()
       {
           string baseString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
           StringBuilder sb = new StringBuilder();
           Random random = new Random();
           int length = 32;
           for (int i = 0; i < length; i++)
           {
               int ranNum = random.Next(35);
               if (sb.Length == 0 || sb.Length < 32)
               {
                   sb.Append(baseString.Substring(ranNum, 62 - ranNum).ToString());
               }
               else if (sb.Length >= 32)
               {
                   return sb.ToString().Substring(0, 32);
               }

           }

           return sb.ToString(0, 32);
       }

       public static Dictionary<string, string> RefreshToken(string id)
       {
           string x = RandomString();

           if (tokenDic.ContainsKey(id))
           {
               tokenDic[id] = x;
           }
           else
           {
               tokenDic.Add(id, x);
           }

           return tokenDic;
       }
    }
}
